<?php
namespace Admin\Controller;
class ManagerController extends CommonController {
	
	 /*
	 *  管理员列表
	 *
	 */
	 public function index(){
	
		$act=I('get.act');
		
	 
       
        if (!empty($act)&&$act==='search') {
		   $keywords=trim(urldecode(I('keywords')));	
           $condition=array();
           $con=array();
           if(!empty($keywords)){
			 $condition['arealname']=array('like','%'.$keywords.'%');
		     $condition['atel']=array('like','%'.$keywords.'%');
			 $condition['_logic']='OR';
           }
         $con['keywords']=$keywords;
        }
      
       
		
		$count=M('admin')->where($condition)->count();
		
        $p=getpage($count,9);
		 
		 foreach($con as $key=>$val) 
		 {
           $p->parameter[$key]   = urlencode($val);
         } 
		 
		 $manager_arr=M('admin')->where($condition)->order('aid desc')->limit($p->firstRow.','.$p->listRows)->select();
		 if($manager_arr){
			 foreach ($manager_arr as $key => $value) {
				 # code...
				 $ee = M('branch')->find($value['dname']);
				 $manager_arr[$key]['dname_name'] = $ee['brank_name'];
			 }
		 }
		 $this->manager=$manager_arr;
		 
		 $this->assign('page',$p->show());
	 
         $this->assign('con',$con);
		 
		 $this->display();
	 }
	 /*
	*
	*  添加+修改
	*
	*
	*/
	public function add(){
		
		 
	    $act=I('act','');
		
		$id=I('id',0,'intval');
		

		if($act=='add')
		{
			$arealname=I('post.arealname');
			$schoolname=I('dname');
			$aname=I('aname');
			$apwd=I('post.apwd');
		 
			!is_empty($arealname,$aname)?message(2,L('_IS_EMPTY_')):''; 
			
			
           
            $arr=array(
			  'arealname'=>$arealname,
			  'dname'=>$schoolname,
			  'aname'=>$aname,
              'apwd'=>md5($apwd.C('SALT')),
              'atime'=>time(),
			); 
			
			$is_exist=M('admin')->where(array('aname'=>$aname,'aid'=>array('neq',$id)))->find();
		   
			!empty($is_exist)?message(2,L('_IS_EXIST_')):'';
		 
		    if(!empty($id)){
			    unset($arr['atime']);
			    if(empty($apwd)) unset($arr['apwd']);
				$lastId=M('admin')->where(array('aid'=>$id))->save($arr);
				
			}else{
			     if(empty($apwd)) message(2,L('_IS_EMPTY_'));
				 $lastId=M('admin')->add($arr);
			}
	 
			 !empty($lastId)?message(1,L('_DO_SUCCEED_'),U('Manager/index')):message(2,L('_DO_FAILED_'));
			  
		}

		$edit=!empty($id)?M('admin')->where(array('aid'=>$id))->find():'';
		//门店查询
		$branch = M('branch')->select();
		$this->assign('branch',$branch);
		$this->edit=$edit;
		$this->display(); 
	}
    
	 /*
	*
	* 删除用户
	*
	*/
	public function del(){
		
        $id=I('post.id',0);

        if(!empty($id)){
			
			$id_arr=is_array($id)?$id:array($id);
		   	
			foreach($id_arr as $k=>$v){
				
				  
				$delId=M('admin')->where(array('aid'=>$v))->delete();
				
				if(empty($delId)) continue;
	  
			}
			
			message(1,L('_DO_SUCCEED_'),U('Manager/index'));
		}		
		message(2,L('_DO_FAILED_'));
	}
	 
 
	 
	
}

 